{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b82e68b1",
   "metadata": {},
   "source": [
    "# 第七周学习笔记\n",
    "\n",
    "## 列表方法总结\n",
    "\n",
    "|  序号  | 方法 | 功能 |\n",
    "| :----: |  :----  |  :----  |\n",
    "| 1 |list.append(obj)|在列表末尾添加新的对象|\n",
    "| 2 |list.count(obj)|统计某个元素在列表中出现的次数|\n",
    "| 3 |list.extend(seq)|在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）|\n",
    "| 4 |list.index(obj)|从列表中找出某个值第一个匹配项的索引位置|\n",
    "| 5 |list.insert(index, obj)|将对象插入列表|\n",
    "| 6 |list.pop([index=-1])|移除列表中的一个元素（默认最后一个元素），并且返回该元素的值|\n",
    "| 7 |list.remove(obj)|移除列表中某个值的第一个匹配项|\n",
    "| 8 |list.reverse()|反向列表中元素|\n",
    "| 9 |list.sort( key=None, reverse=False)|对原列表进行排序|\n",
    "| 10 |list.clear()|清空列表|\n",
    "| 11 |list.copy()|复制列表|\n",
    "\n",
    "## 列表实战练习\n",
    "- 目标： “Don't panic” ==> \"on tap\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b97303a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 方法一： 使用list的方法完成\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "# 1.删除后面四个元素 a n i c\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "# 2. 删除 D  ‘\n",
    "plist.remove('D')\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# 3. 数学案例： 计算 （(a+b)*(c+d)）\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "#['o','n','t',' '][ a    ,   p     ]\n",
    "print(plist)\n",
    "\n",
    "# 4. 数据插入  实现：’t‘ 和’ ‘ 的位置转换\n",
    "# plist.insert(2,plist.pop(3))\n",
    "# print(plist)\n",
    "# print(plist.pop(2))  # 1，代表删除的内容的值 2，他删除了plist的这个值\n",
    "plist.insert(3,plist.pop(2))\n",
    "print(plist)\n",
    "\n",
    "# 5. list to str\n",
    "new_phrase = ''.join(plist)\n",
    "\n",
    "print(plist)\n",
    "print(new_phrase)\n",
    "# plist 改变了原先的状态么？ 列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，请先用copy()复制数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "927a18c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 方法二： 使用切片的方法实现\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6da1fe16",
   "metadata": {},
   "source": [
    "## 元组\n",
    "- Python 的元组与列表类似，不同之处在于元组的元素不能修改。  \n",
    "- 元组使用小括号 ( )，列表使用方括号 [ ]。  \n",
    "- 元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "92fee8f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n",
      "('Google', 'Runoob', 1997, 2000)\n",
      "(1, 2, 3, 4, 5)\n",
      "('a', 'b', 'c', 'd')\n"
     ]
    }
   ],
   "source": [
    "#创建空元组\n",
    "tup0 = ()\n",
    "tup1 = ('Google', 'Runoob', 1997, 2000)\n",
    "tup2 = (1, 2, 3, 4, 5 )\n",
    "tup3 = \"a\", \"b\", \"c\", \"d\"   #  不需要括号也可以\n",
    "type(tup3)\n",
    "print(tup0)\n",
    "print(tup1)\n",
    "print(tup2)\n",
    "print(tup3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07d9998b",
   "metadata": {},
   "source": [
    "- 访问元组：元组可以使用下标索引来访问元组中的值；\n",
    "- 修改元组：元组中的元素值是不允许修改的，但我们可以对元组进行连接组合；\n",
    "- 删除元组：元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f28e4017",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tup1[0]:  Google\n",
      "tup2[1:5]:  (2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "# 访问元组\n",
    "tup1 = ('Google', 'Runoob', 1997, 2000)\n",
    "tup2 = (1, 2, 3, 4, 5, 6, 7 )\n",
    " \n",
    "print (\"tup1[0]: \", tup1[0])\n",
    "print (\"tup2[1:5]: \", tup2[1:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2728def6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 34.56, 'abc', 'xyz')\n"
     ]
    }
   ],
   "source": [
    "#修改元组\n",
    "tup1 = (12, 34.56)\n",
    "tup2 = ('abc', 'xyz')\n",
    " \n",
    "# 以下修改元组元素操作是非法的。\n",
    "# tup1[0] = 100\n",
    " \n",
    "# 创建一个新的元组\n",
    "tup3 = tup1 + tup2\n",
    "print (tup3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d3d6fb0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Google', 'Runoob', 1997, 2000)\n",
      "删除后的元组 tup : \n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'tup' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-b09cb012a90e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mtup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"删除后的元组 tup : \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#这样就会报错说元组不在\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'tup' is not defined"
     ]
    }
   ],
   "source": [
    "#删除元组\n",
    "tup = ('Google', 'Runoob', 1997, 2000)\n",
    " \n",
    "print (tup)\n",
    "del tup\n",
    "print (\"删除后的元组 tup : \")\n",
    "print (tup)#这样就会报错说元组不在"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54d6c7eb",
   "metadata": {},
   "source": [
    "## 元组内置函数及案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "75e9f44d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# len(tuple)：计算元组元素个数。\n",
    "tuple1 = ('Google', 'Runoob', 'Taobao')\n",
    "len(tuple1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "26f3735c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# max(tuple)：返回元组中元素最大值。\n",
    "tuple2 = ('5', '4', '8')\n",
    "max(tuple2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c93293cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# min(tuple)：返回元组中元素最小值。\n",
    "tuple2 = ('5', '4', '8')\n",
    "min(tuple2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d96d14c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Google', 'Taobao', 'Runoob', 'Baidu')\n"
     ]
    }
   ],
   "source": [
    "# tuple(iterable)：将可迭代系列转换为元组。\n",
    "list1= ['Google', 'Taobao', 'Runoob', 'Baidu']\n",
    "tuple1=tuple(list1)\n",
    "print(tuple1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "da86f475",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-f425759f347d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 关于元组是不可变的：所谓元组的不可变指的是元组所指向的内存中的内容不可变。\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mtup\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'u'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'n'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'o'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'o'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'b'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'g'\u001b[0m     \u001b[1;31m# 不支持修改元素\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[0mid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[1;33m)\u001b[0m     \u001b[1;31m# 查看内存地址\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mtup\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "# 关于元组是不可变的：所谓元组的不可变指的是元组所指向的内存中的内容不可变。\n",
    "tup = ('r', 'u', 'n', 'o', 'o', 'b')\n",
    "tup[0] = 'g'     # 不支持修改元素\n",
    "id(tup)     # 查看内存地址\n",
    "tup = (1,2,3)\n",
    "id(tup)\n",
    "# 内存地址不一样了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "67d45f51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2827218384512"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关于元组是不可变的：所谓元组的不可变指的是元组所指向的内存中的内容不可变。\n",
    "tup = ('r', 'u', 'n', 'o', 'o', 'b')\n",
    "#tup[0] = 'g'     # 不支持修改元素\n",
    "id(tup)     # 查看内存地址\n",
    "tup = (1,2,3)\n",
    "id(tup)\n",
    "# 内存地址不一样了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ba72e6f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
